# -*- coding: utf-8 -*-
"""
统计对话框相关功能模块
"""
import sqlite3
from PyQt5.QtWidgets import QMainWindow
from StatsWin import Ui_MainWindow as StatsWinUi


def show_stats_dialog(window):
    """
    显示统计信息对话框
    
    Args:
        window: 父窗口组件
    """
    # 创建统计对话框实例
    stats_window = QMainWindow(window)
    ui = StatsWinUi()
    ui.setupUi(stats_window)
    
    # 统计数据并填充界面
    _fill_stats_data(ui)
    
    # 显示对话框
    stats_window.show()
    return stats_window


def _fill_stats_data(ui):
    """
    从数据库获取统计数据并填充到界面
    
    Args:
        ui: StatsWin界面实例
    """
    try:
        # 连接数据库
        conn = sqlite3.connect('nginx_logs.db')
        cursor = conn.cursor()
        
        # 统计IP相关数据
        # 高危IP数量 (point >= 0.7)
        cursor.execute("SELECT COUNT(DISTINCT ip) FROM nginx_logs WHERE point >= 0.7")
        result = cursor.fetchone()
        high_risk_ips = result[0] if result else 0
        ui.label.setText(f"高危IP数量：{high_risk_ips}")
        
        # 中危IP数量 (0.3 <= point < 0.7)
        cursor.execute("SELECT COUNT(DISTINCT ip) FROM nginx_logs WHERE point >= 0.3 AND point < 0.7")
        result = cursor.fetchone()
        medium_risk_ips = result[0] if result else 0
        ui.label_2.setText(f"中危IP数量：{medium_risk_ips}")
        
        # 正常IP数量 (point < 0.3)
        cursor.execute("SELECT COUNT(DISTINCT ip) FROM nginx_logs WHERE point < 0.3")
        result = cursor.fetchone()
        normal_ips = result[0] if result else 0
        ui.label_3.setText(f"正常IP数量：{normal_ips}")
        
        # 全部IP数量
        cursor.execute("SELECT COUNT(DISTINCT ip) FROM nginx_logs")
        result = cursor.fetchone()
        all_ips = result[0] if result else 0
        ui.label_4.setText(f"全部IP数量：{all_ips}")
        
        # 统计URL相关数据
        # 高危URL数量 (point >= 0.7)
        cursor.execute("SELECT COUNT(string) FROM nginx_logs WHERE point >= 0.7")
        result = cursor.fetchone()
        high_risk_urls = result[0] if result else 0
        ui.label_5.setText(f"高危URL数量：{high_risk_urls}")
        
        # 中危URL数量 (0.3 <= point < 0.7)
        cursor.execute("SELECT COUNT(string) FROM nginx_logs WHERE point >= 0.3 AND point < 0.7")
        result = cursor.fetchone()
        medium_risk_urls = result[0] if result else 0
        ui.label_6.setText(f"中危URL数量：{medium_risk_urls}")
        
        # 正常URL数量 (point < 0.3)
        cursor.execute("SELECT COUNT(string) FROM nginx_logs WHERE point < 0.3")
        result = cursor.fetchone()
        normal_urls = result[0] if result else 0
        ui.label_7.setText(f"正常URL数量：{normal_urls}")
        
        # 全部URL数量
        cursor.execute("SELECT COUNT(string) FROM nginx_logs")
        result = cursor.fetchone()
        all_urls = result[0] if result else 0
        ui.label_8.setText(f"全部URL数量：{all_urls}")
        
        # 获取高危IP TOP10
        # 清空现有列表
        ui.listWidget.clear()
        
        # 查询高危IP TOP10 (按最大攻击概率排序)
        cursor.execute("""
            SELECT ip, address, MAX(point) as max_point 
            FROM nginx_logs 
            GROUP BY ip 
            ORDER BY max_point DESC 
            LIMIT 10
        """)
        
        high_risk_ips = cursor.fetchall()
        
        # 添加到列表中
        for ip, address, risk_score in high_risk_ips:
            ui.listWidget.addItem(f"{ip} - {address} - 攻击概率:{risk_score:.2%}")
        
        # 关闭数据库连接
        conn.close()
        
    except Exception as e:
        print(f"统计数据填充错误: {e}")